//
//  WQNewFutureController.swift
//  sanwuWeibo
//
//  Created by 王先强 on 15/12/9.
//  Copyright © 2015年 王先强. All rights reserved.
//

import UIKit
import SnapKit

private let WQReuseID = "WQReuseID"
private let WQImageCount = 4

class WQNewFutureController: UICollectionViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(WQNewFutureCell.self, forCellWithReuseIdentifier: WQReuseID)

    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {

        return WQImageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(WQReuseID, forIndexPath: indexPath) as! WQNewFutureCell
        cell.backgroundColor = randomColor
        cell.startBtn.hidden = true
        cell.imageIndex = indexPath.item
        return cell
    }
    
    init() {
        let layout = UICollectionViewFlowLayout()
        super.init(collectionViewLayout: layout)
        layout.itemSize = view.bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        collectionView?.showsHorizontalScrollIndicator = false
        print(layout.itemSize)
    }
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        let currentPage = (collectionView?.contentOffset.x)! / scrollView.bounds.width
        print(currentPage)
        let cell = collectionView?.cellForItemAtIndexPath(NSIndexPath(forItem: Int(currentPage), inSection: 0)) as? WQNewFutureCell
        if Int(currentPage) != 3 {

            return
        }
        cell?.btnAnimation()
        
    }
}




class WQNewFutureCell: UICollectionViewCell {
    
    private var imageIndex: Int = 0 {
        didSet {
        imageView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {

        addSubview(imageView)
        addSubview(startBtn)
        startBtn.hidden = true
        imageView.frame = bounds
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_bottom).multipliedBy(0.75)
        }
        startBtn.addTarget(self, action: "startWeibo", forControlEvents: .TouchUpInside)
    }
    
    func startWeibo() {
        
        WQNotificationCenter.postNotificationName(WQSwitchRootViewControllerNotification, object: nil)

        print("weibo")
    }
    

    func btnAnimation() {

        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        startBtn.hidden = false

        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 2.0, options: [], animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                
        }
    }
    lazy var imageView = UIImageView()
    lazy var startBtn = UIButton(backgroundImageName: "new_feature_finish_button", title: "开始体验", color: UIColor.darkGrayColor())
    
}
